How does the “return value” of operator >> of istream class work?

To return a self ref you just return *this You don't actually declare an internal self reference. You can have reference members, but you must initialize them in the constructor initializer list: class Foo { Foo(int& some_int_ref) :my_ref_member(some_int_ref).

To return a self ref you just return *this. You don't actually declare an internal self reference. You can have reference members, but you must initialize them in the constructor initializer list: class Foo { Foo(int& some_int_ref) :my_ref_member(some_int_ref) istream has another overload to determine its boolean value.

Example of a chainable member (like operator >>, but in this case just an ordinary function) and giving a class a boolean value (although the latter is a complex subject worthy of its own subject): #include class X { bool B; public: X() :B(false) { } X& toggle() { B =! B; return *this; } operator void*() { return B? This : 0; } }; int main() { X x; x.toggle().toggle().toggle(); if (x) std::cout ".

Thanks for clear explanation. So if I want to implement a similar technique like istream class, what should I provide in my class? – Chan Dec 26 '10 at 8:37.

Void*. The loop test resolves to while (inf. Operator void*()!

= NULL). How does a reference can be evaluated as true or false? But supporting conversion to bool or something convertible to bool.

How does the internal implementation of istream actually work? It just returns a reference to itself (return *this) so it can support chaining.

Thanks! So I have to provide the void*() operator in order to put that statement inside the while loop? – Chan Dec 26 '10 at 8:36 You needn't call it explicitly (as you've already seen).

It's a conversion operator. C++ calls it automatically here because it finds the object being used in a boolean context. If there was an operator bool it would call that instead, but there isn't, so it calls operator void* which is convertible to bool.It's good to understand this stuff because it can seriously bite you in the ass.

:) – Mud Dec 26 '10 at 8:40 Thanks Mud, I got it ;) – Chan Dec 26 '10 at 8:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send